package com.mall507.fivepeaks.activity.mine;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.support.v4.app.ActivityCompat;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.google.gson.Gson;
import com.hzh.frame.comn.ItemDecoration.BaseGridSpacingItemDecoration;
import com.hzh.frame.comn.annotation.ViewInject;
import com.hzh.frame.comn.callback.HttpCallBack;
import com.hzh.frame.core.BaseSP;
import com.hzh.frame.core.HttpFrame.BaseHttp;
import com.hzh.frame.ui.activity.BaseUI;
import com.hzh.frame.util.AndroidUtil;
import com.hzh.frame.util.StatusBarUtil;
import com.hzh.frame.widget.rxbus.RxBus;
import com.hzh.frame.widget.xrecyclerview.BaseRecyclerAdapter;
import com.hzh.frame.widget.xrecyclerview.RecyclerViewHolder;
import com.mall507.comn.matisse.GifSizeFilter;
import com.mall507.comn.model.ConvenientLifeZhaoPinInfo;
import com.mall507.comn.model.MineSettingEditData;
import com.mall507.comn.model.UploadImage;
import com.mall507.comn.model.User;
import com.mall507.comn.tools.UserTools;
import com.mall507.fivepeaks.R;
import com.mall507.fivepeaks.activity.find.FindSubmitReleaseUI;
import com.mall507.fivepeaks.activity.release.ReleaseFM;
import com.mall507.fivepeaks.util.ImageUtil;
import com.mall507.fivepeaks.util.UriUtil;
import com.mall507.fivepeaks.util.Util;
import com.mall507.fivepeaks.widget.imageview.CircleImageView;
import com.mall507.fivepeaks.widget.xdialog.XMineChooseDialog;
import com.mall507.fivepeaks.widget.xdialog.XRadioGroupDialog;
import com.mall507.fivepeaks.widget.xpopupwindow.XStoreListPW;
import com.yalantis.ucrop.UCrop;
import com.yalantis.ucrop.UCropActivity;
import com.zhihu.matisse.Matisse;
import com.zhihu.matisse.MimeType;
import com.zhihu.matisse.engine.impl.PicassoEngine;
import com.zhihu.matisse.filter.Filter;
import com.zhihu.matisse.internal.entity.CaptureStrategy;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import static com.mob.MobSDK.getContext;

@Route(path = "/mine/SettingEditDataUI")
public class SettingEditDataUI extends BaseUI implements View.OnClickListener {
    @ViewInject(R.id.rl_avatar) //用户头像
    RelativeLayout rl_avatar;
    @ViewInject(R.id.rl_nickname) //用户昵称
    RelativeLayout rl_nickname;
    @ViewInject(R.id.rl_sex) //用户性别
    RelativeLayout rl_sex;
    @ViewInject(R.id.rl_signature) //用户
    RelativeLayout rl_signature;
    @ViewInject(R.id.user_avatar)//用户头像
    CircleImageView user_avatar;
    @ViewInject(R.id.nickname)
    TextView nickname;
    @ViewInject(R.id.sex)
    TextView sex;
    @ViewInject(R.id.signature)
    TextView signature;
    @ViewInject(R.id.image_upload_recyclerView)
    RecyclerView imageUploadRecyclerView;

    Dialog mDialog;
    private TextView cancel,confirm;
    private RadioButton checkone,checktwo;
    private static final int REQUEST_IMAGE = 1;// 图片选择
    private static final int REQUEST_CROP = 2;// 图片剪裁

    List<HashMap<String, Object>> radioGroupList = new ArrayList<>();
    HashMap<String, Object> map = new HashMap<>();
    private SettingEditDataUI mSettingEditDataUI;

    @Override
    protected void onCreateBase() {
        setContentView(R.layout.ui_mine_setting_editdata);
        getTitleView().setContent("编辑资料");
        rl_avatar.setOnClickListener(this);
        rl_nickname.setOnClickListener(this);
        rl_sex.setOnClickListener(this);
        rl_signature.setOnClickListener(this);
        map.put("sex", "男");
        map.put("sex", "女");
        radioGroupList.add(map);
        mSettingEditDataUI = new SettingEditDataUI();

        String nicknames = BaseSP.getInstance().getString("nickname");
        String signatures = BaseSP.getInstance().getString("signature");
        if (nicknames != null){
            nickname.setText(nicknames);
        }
        
        if (signatures != null){
            signature.setText(signatures);
        }
        loadUserInfo();
    }

    public void loadUserInfo() {
        if (UserTools.getInstance().getIsLogin()) {
            User user = UserTools.getInstance().getUser();
            user_avatar.setImageURI(user.getHead());
            nickname.setText(user.getNickName());
            sex.setText(user.getSexPacking());
            if (user.getPersonalSign().equals("null") || user.getPersonalSign() == null){
                signature.setHint("暂无个性签名");
            }else {
                signature.setText(user.getPersonalSign());
            }
        } else {
            user_avatar.setImageResource(R.mipmap.base_image_face);
        }
    }
    
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.rl_avatar:
                ARouter.getInstance().build("/mine/MineHeadUI").navigation(); //头像
                break;
            case R.id.rl_nickname:
                ARouter.getInstance().build("/mine/SettingEditnicknameUI").navigation(); //修改昵称
                break;
            case R.id.rl_sex:
                chooseSex();
                break;
            case R.id.rl_signature:
                ARouter.getInstance().build("/mine/SettingEditsignatureUI").withString("signature",signature.getText().toString()).navigation(); //个性签名
                break;
        }
    }

    private void chooseSex(){
        mDialog = new Dialog(SettingEditDataUI.this,R.style.chooseDialog);
        LayoutInflater inflater = LayoutInflater.from(getContext());
        View view = inflater.inflate(R.layout.view_xdialog_editsex, null);
        int width = AndroidUtil.getWindowWith();
        int height = AndroidUtil.getWindowHeight();
        ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(width, height);
        mDialog.setContentView(view, layoutParams);
        TextView close = view.findViewById(R.id.close);
        cancel = view.findViewById(R.id.cancel);
        confirm = view.findViewById(R.id.confirm);
        checkone = view.findViewById(R.id.checkone);
        checktwo = view.findViewById(R.id.checktwo);
        mDialog.setCanceledOnTouchOutside(true);
        mDialog.show();
        if (sex.getText().toString().equals("女")){
            checktwo.setChecked(true);
        }else {
            checkone.setChecked(true);
        }
        close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDialog.dismiss();
            }
        });
        
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDialog.dismiss();
            }
        });

        confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (checkone.isChecked() == true){
                    sex.setText(checkone.getText().toString());
                    JSONObject params = new JSONObject();
                    try {
                        params.put("type", 3);
                        params.put("sex", 0);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    BaseHttp.getInstance().query("Me/Edit", params, new HttpCallBack() {
                        @Override
                        public void onSuccess(JSONObject response) {
                            if (200 == response.optInt("code")){
                                
                            }
                            alert(response.optString("msg"));
                            super.onSuccess(response);
                        }
                    });
                }else if (checktwo.isChecked() == true){
                    sex.setText(checktwo.getText().toString());
                    JSONObject params = new JSONObject();
                    try {
                        params.put("type", 3);
                        params.put("sex", 1);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    BaseHttp.getInstance().query("Me/Edit", params, new HttpCallBack() {
                        @Override
                        public void onSuccess(JSONObject response) {
                            if (200 == response.optInt("code")){
                                alert("性别更改成功");
                            }
                            super.onSuccess(response);
                        }
                    });
                }
                mDialog.dismiss();
            }
        });
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        loadUserInfo();
    }
}
